//
//  HNSH_ChatHomeViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/16.
//

#import "HNSH_ChatHomeViewController.h"
#import "ST_PageView.h"
#import "RXPopMenu.h"

//vc
#import "HNSH_CreateChatGroupViewController.h"      //创建群聊
#import "HNSH_SearchFriendsViewController.h"        //搜索好友
#import "HZZX_ChatScanViewController.h"             //扫一扫

#import "HNSH_ChatFriendViewController.h"

@interface HNSH_ChatHomeViewController ()
/* buttons */
@property (nonatomic, strong) NSMutableArray *buttons;
/* selectImage */
@property (nonatomic, strong) UIImage *selectImage;
/* chatAlert */
@property (nonatomic, strong) UILabel *chatAlert;
/* txlAlert */
@property (nonatomic, strong) UILabel *txlAlert;
@end

@implementation HNSH_ChatHomeViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"KUpLoadChatListData" object:nil];
    [self loadUnreadCount];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor appBGColor];
    
    [self showNavTitle:@"我的聊天" showLeftItem:YES];
    self.navView.backgroundColor = [UIColor appBGColor];
    
    
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addBtn setImage:[UIImage imageNamed:@"chat_home_nav_right"] forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(showPopWithBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn];
    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.width.mas_equalTo(44);
        make.height.mas_equalTo(44);
        make.top.mas_equalTo(kStatusBarHeight);
    }];
    
    NSArray *array1 = @[@"HNSH_ChatListViewController",
                        [HNSH_ChatFriendViewController new]
    ];
    ST_PageView *pageView = [[ST_PageView alloc]initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight-kNavBarAndStatusBarHeight) withTitles:@[@"聊天",@"通讯录"] withViewControllers:array1 withParameters:@[@"0",@"1"]];
    pageView.isAnimated = NO;
    pageView.selectedColor = [UIColor appBlackLabelColor];
    pageView.unselectedColor = [UIColor appGrayLabelColor];
    pageView.leftSpace = pageView.rightSpace = 44;
//    pageView.lineWidth = 21;
    //pageView.isTranslucent = YES;
    pageView.isShowTopTabBottomLine = NO;
    pageView.isAverage = YES;
    pageView.font = [UIFont systemFontOfSize:16];
    pageView.scrollView.backgroundColor = [UIColor appBGColor];
    [self.view addSubview:pageView];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadUnreadCount) name:@"kRefreshUnreadCount" object:nil];
    
    //消息列表未读
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(unreadCountNoti:) name:@"KUnreadCountNoti" object:nil];
    
    [self.view addSubview:self.chatAlert];
    [self.view addSubview:self.txlAlert];
}

//通讯录加好友未读
- (void)loadUnreadCount{
    [HNSH_ChatNetServer requestRongCloudFriendshipUnreadCountSuccess:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            NSInteger count = [responseObject.resultset integerValue];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"KTXLUnreadCountNiti" object:self userInfo:@{@"count":@(count)}];
            self.txlAlert.hidden = count <= 0;
            if (count > 0) {
                if (count > 99) {
                    self.txlAlert.text = [NSString stringWithFormat:@"99+"];
                } else {
                    self.txlAlert.text = [NSString stringWithFormat:@"%ld",count];
                }
                self.txlAlert.text = [NSString stringWithFormat:@"%ld",count];
                CGFloat width = [self.txlAlert.text widthForFont:self.txlAlert.font];
                self.txlAlert.width = width+10;
            }
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

//聊天数量
- (void)unreadCountNoti:(NSNotification *)sender {
    NSDictionary *dic = sender.userInfo;
    NSInteger count = [dic[@"count"] integerValue];
    self.chatAlert.hidden = count <= 0;
    if (count > 0) {
        if (count > 99) {
            self.chatAlert.text = [NSString stringWithFormat:@"99+"];
        } else {
            self.chatAlert.text = [NSString stringWithFormat:@"%ld",count];
        }
        CGFloat width = [self.chatAlert.text widthForFont:self.chatAlert.font];
        self.chatAlert.width = width+10;
    }
}

- (void)showPopWithBtn:(UIButton *)sender{
    NSArray * showItems = @[[RXPopMenuItem itemTitle:@"发起群聊" image:@"chat_home_alert_news"],
                            [RXPopMenuItem itemTitle:@"添加好友" image:@"chat_home_alert_add_friend"],
                            [RXPopMenuItem itemTitle:@"扫一扫" image:@"chat_home_alert_qrcode"]
    ];
    RXPopMenu * menu = [RXPopMenu menu];
    menu.backColor = [UIColor appGray666LabelColor];
    menu.titleColor = [UIColor appWhiteLabelColor];
    [menu showBy:sender withItems:showItems];
    __weak typeof(self) weak = self;
    menu.itemActions = ^(RXPopMenuItem *item) {
        __strong typeof(self) strong = weak;
        NSLog(@"%@", item.title);
        if ([item.title isEqualToString:@"发起群聊"]) {
            HNSH_CreateChatGroupViewController * vc = [HNSH_CreateChatGroupViewController new];
            [strong.navigationController pushViewController:vc animated:YES];
        }
        if ([item.title isEqualToString:@"添加好友"]) {
            HNSH_SearchFriendsViewController * vc = [HNSH_SearchFriendsViewController new];
            [strong.navigationController pushViewController:vc animated:YES];
        }
        if ([item.title isEqualToString:@"扫一扫"]) {
            HZZX_ChatScanViewController * vc = [HZZX_ChatScanViewController new];
            [strong.navigationController pushViewController:vc animated:YES];
        }
    };
}

- (UIImage *)selectImage {
    if (!_selectImage) {
        _selectImage = [UIImage createImageSize:CGSizeMake((kScreenWidth-49)/2, 26) gradientColors:@[[UIColor colorWithHexString:@"#218AF9"],[UIColor colorWithHexString:@"#59DEAA"]] percentage:@[@0,@1]];
    }
    return _selectImage;
}

- (UILabel *)chatAlert {
    if (!_chatAlert) {
        _chatAlert = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth/3+16, kNavBarAndStatusBarHeight+5, 18, 18)];
        _chatAlert.font = [UIFont systemFontOfSize:13];
        _chatAlert.textColor = [UIColor whiteColor];
        _chatAlert.backgroundColor = [UIColor colorWithHexString:@"#FF3E35"];
        _chatAlert.textAlignment = NSTextAlignmentCenter;
        _chatAlert.layer.cornerRadius = 9;
        _chatAlert.layer.masksToBounds = YES;
        _chatAlert.hidden = YES;
    }
    return _chatAlert;
}

- (UILabel *)txlAlert {
    if (!_txlAlert) {
        _txlAlert = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth/3*2+24, kNavBarAndStatusBarHeight+5, 18, 18)];
        _txlAlert.font = [UIFont systemFontOfSize:13];
        _txlAlert.textColor = [UIColor whiteColor];
        _txlAlert.backgroundColor = [UIColor colorWithHexString:@"#FF3E35"];
        _txlAlert.textAlignment = NSTextAlignmentCenter;
        _txlAlert.layer.cornerRadius = 9;
        _txlAlert.layer.masksToBounds = YES;
        _txlAlert.hidden = YES;
    }
    return _txlAlert;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
